package hgx.spring.cloud.lession.orderserver.controller;/**
 * Created by liujun on 2018/3/15.
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

import hgx.spring.cloud.lession.orderserver.dao.OrderDaoImpl;
import hgx.spring.cloud.lession.orderserver.vo.OrderVo;
import hgx.spring.cloud.lession.orderserver.vo.ReponseOrderListVo;
import hgx.spring.cloud.lession.orderserver.vo.ReponseOrderVo;

/**
 * @author Administrator
 * @create 2018-03-15 14:41
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderDaoImpl orderDao;



    @PostMapping(value = "/create")
    public ReponseOrderVo createOrder(@RequestBody OrderVo orderVo) {
        System.out.println("请求参数:  " + orderVo.toString());
        ReponseOrderVo reponseOrderVo = new ReponseOrderVo();
        if (orderVo == null) {
            return reponseOrderVo;
        }

        orderVo.setCreateDate(new Date());
        orderVo.setUpdateDate(new Date());
        orderDao.createOrder(orderVo);

        reponseOrderVo.setOrderVo(orderVo);
        reponseOrderVo.success();

        return reponseOrderVo;
    }

    @GetMapping("/{orderNo}")
    public ReponseOrderVo getOrderById(@PathVariable("orderNo")Long orderNo)  {

        System.out.println("订单模块收到请求参数 orderNo: "+orderNo);
        ReponseOrderVo reponseOrderVo=new ReponseOrderVo();
        if(orderNo == null){
            return  reponseOrderVo;
        }

        OrderVo orderVo=orderDao.getOrderVoById(orderNo);
        if(orderVo !=null){
            reponseOrderVo.setOrderVo(orderVo);
            reponseOrderVo.success();
        }

        System.out.println("订单模块响应： "+reponseOrderVo.toString());
        return reponseOrderVo;
    }


    @GetMapping("/getOrderByUserId")
    public ReponseOrderListVo getOrderByUserId(@RequestParam Long userId) {

        ReponseOrderListVo reponse=new ReponseOrderListVo();
        if(userId == null){
            return  reponse;
        }

        List<OrderVo> orderVos=orderDao.findOrderByUserId(userId);
        if(orderVos !=null){
            reponse.setOrderVos(orderVos);
            reponse.success();
        }

        System.out.println("订单模块响应： "+reponse.toString());
        return reponse;
    }

    @GetMapping("")
    public ReponseOrderListVo findOrders() throws InterruptedException {
        ReponseOrderListVo reponseOrderListVo=new ReponseOrderListVo();
        List<OrderVo> orderVos=orderDao.findAll();
        if(orderVos !=null) {
            reponseOrderListVo.setOrderVos(orderVos);
            reponseOrderListVo.success();
        }

        System.out.println("订单模块响应： "+reponseOrderListVo.toString());
        return reponseOrderListVo;
    }
}
